<?php

namespace App\Http\Controllers;

use App\Http\Requests\TaskRequests;
use App\Models\Record;
use App\Models\Task;
use App\Repositories\ProjectRepository;
use App\Repositories\TaskRepository;
use App\Services\Deploy;
use Illuminate\View\View;

class TaskController extends Controller
{
    private $_taskRepository;

    public function __construct()
    {
        $this->_taskRepository = new TaskRepository();
    }

    /**
     * 项目配置列表
     * @return View
     */
    public function index()
    {
        $projectRepository = new ProjectRepository();
        $paginator = $this->_taskRepository->getPageList();
        $activeProjects = $projectRepository->getActiveProjects();
        $title = request('title', '');
        $projectId = request('project_id', '');
        return view('task.index')->with(compact(["paginator", 'title', 'activeProjects', 'projectId']));
    }

    /**
     * 选择部署的项目配置
     * @return \Illuminate\Contracts\View\Factory|View
     */
    public function select()
    {
        $projectRepository = new ProjectRepository();
        $activeProjects = $projectRepository->getActiveProjects();
        $testProjects = [];
        $productProjects = [];
        foreach ($activeProjects as $project) {
            if ($project->level == config("constants.project.level.test")) {
                array_push($testProjects, $project);
            } else {
                array_push($productProjects, $project);
            }
        }
        return view('task.select')->with(compact(["testProjects", "productProjects"]));
    }

    /**
     * 新增上线单视图
     * @param $pid
     * @return \Illuminate\Contracts\View\Factory|View
     */
    public function create($pid)
    {
        return view('task.create')->with(compact("pid"));
    }

    /**
     * 添加上线单
     * @param TaskRequests $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(TaskRequests $request)
    {
        $this->_taskRepository->store($request);
        return redirect('task');
    }

    /**
     * 删除上线单
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->_taskRepository->destroy($id);
        return redirect('task');
    }

    /**
     * 部署任务视图
     * @param $tid
     * @return \Illuminate\Contracts\View\Factory|View
     */
    public function deploy($tid)
    {
        $task = Task::find($tid);
        if ($task) {
            return view('task.deploy')->with(compact("task"));
        } else {
            abort(404);
        }
    }

    /**
     * 发起上线
     * @return \Illuminate\Http\JsonResponse
     */
    public function StartDeploy()
    {
        $code = 0;
        $msg = "";
        try {
            $tid = request('tid', '');
            $deploy = new Deploy($tid);
            $deploy->StartDeploy();
        } catch (\Exception $e) {
            $code = -1;
            $msg = $e->getMessage();
        }
        return response()->json(["code" => $code, "msg" => $msg]);
    }

    /**
     * 获取上线进度
     * @param $tid
     * @return \Illuminate\Http\JsonResponse
     */
    public function GetProcess($tid)
    {
        $record = Record::
        where("task_id", $tid)
            ->orderBy("id", 'desc')
            ->first();
        if ($record) {
            $record = $record->toArray();
            $record['memo'] = stripslashes($record['memo']);
            $record['command'] = stripslashes($record['command']);
        }
        return response()->json(["data" => $record]);
    }

    /**
     * 生成回滚任务
     * @param $taskId
     * @return \Illuminate\Http\JsonResponse
     */
    public function rollback($taskId)
    {
        $code = 0; $msg = ""; $id = "";
        try {
            $deploy = new Deploy($taskId);
            $id = $deploy->Rollback();
        } catch (\Exception $e) {
            $code = -1;
            $msg = $e->getMessage();
        }
        return response()->json(["code" => $code, "msg" => $msg, "id" => $id]);
    }
}
